package com.example.autojob.skeleton.framework.network.handler.server;

import com.example.autojob.skeleton.annotation.AutoJobRPCService;
import com.example.autojob.skeleton.annotation.AutoJobRPCServiceScan;
import com.example.autojob.skeleton.framework.boot.AutoJobApplication;
import org.reflections.Reflections;
import org.reflections.scanners.Scanners;

import java.util.Set;

/**
 * 服务扫描器
 *
 * @Author Huang Yongxiang
 * @Date 2022/09/17 18:09
 */
public class ServiceScanner {
    private final Reflections reflections;

    public Set<Class<?>> scan() {
        return reflections.getTypesAnnotatedWith(AutoJobRPCService.class);
    }

    public ServiceScanner() {
        Class<?> app = AutoJobApplication
                .getInstance()
                .getApplication();
        if (app != null) {
            AutoJobRPCServiceScan serviceScan = app.getAnnotation(AutoJobRPCServiceScan.class);
            if (serviceScan == null) {
                reflections = new Reflections(Scanners.TypesAnnotated);
            } else {
                reflections = new Reflections(serviceScan.value(), Scanners.TypesAnnotated);
            }
        } else {
            reflections = new Reflections(Scanners.TypesAnnotated);
        }
    }
}
